package com.schedule.controller;

import com.schedule.common.result.Result;
import com.schedule.dto.AutoScheduleDto;
import com.schedule.service.ScheduleService;
import com.schedule.vo.ScheduleResultVo;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.time.LocalDate;
import java.util.List;

/**
 * 排课控制器
 */
@RestController
@RequestMapping("/api/schedule")
@RequiredArgsConstructor
public class ScheduleController {

    private final ScheduleService scheduleService;

    /**
     * 自动排课
     */
    @PostMapping("/auto")
    public Result<ScheduleResultVo> autoSchedule(@Valid @RequestBody AutoScheduleDto autoScheduleDto) {
        ScheduleResultVo result = scheduleService.autoSchedule(autoScheduleDto);
        return Result.success(result);
    }

    /**
     * 预览排课结果
     */
    @PostMapping("/preview")
    public Result<ScheduleResultVo> previewSchedule(@Valid @RequestBody AutoScheduleDto autoScheduleDto) {
        ScheduleResultVo result = scheduleService.previewSchedule(autoScheduleDto);
        return Result.success(result);
    }

    /**
     * 确认排课结果
     */
    @PostMapping("/confirm")
    public Result<Boolean> confirmSchedule(@RequestParam Long classId, @RequestParam Long rootCourseId) {
        Boolean result = scheduleService.confirmSchedule(classId, rootCourseId);
        return Result.success(result);
    }

    /**
     * 获取班级课表
     */
    @GetMapping("/class/{classId}")
    public Result<ScheduleResultVo> getClassSchedule(@PathVariable Long classId, 
                                                    @RequestParam(required = false) Long rootCourseId) {
        ScheduleResultVo result = scheduleService.getClassSchedule(classId, rootCourseId);
        return Result.success(result);
    }

    /**
     * 获取教师课表
     */
    @GetMapping("/teacher/{teacherId}")
    public Result<List<ScheduleResultVo.ScheduleItemVo>> getTeacherSchedule(
            @PathVariable Long teacherId,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        
        List<ScheduleResultVo.ScheduleItemVo> result = scheduleService.getTeacherSchedule(teacherId, startDate, endDate);
        return Result.success(result);
    }

    /**
     * 获取日历数据
     */
    @GetMapping("/calendar")
    public Result<List<ScheduleResultVo.ScheduleItemVo>> getCalendarData(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        
        // 获取指定日期的所有课程安排
        LocalDate endDate = date.plusDays(1);
        List<ScheduleResultVo.ScheduleItemVo> result = scheduleService.getTeacherSchedule(null, date, endDate);
        return Result.success(result);
    }

    /**
     * 调整课程安排
     */
    @PutMapping("/adjust")
    public Result<Boolean> adjustSchedule(@RequestParam Long scheduleId,
                                         @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate newDate,
                                         @RequestParam Integer newTimeSlot,
                                         @RequestParam Long newTeacherId) {
        
        Boolean result = scheduleService.adjustSchedule(scheduleId, newDate, newTimeSlot, newTeacherId);
        return Result.success(result);
    }

    /**
     * 检查排课冲突
     */
    @PostMapping("/check-conflicts")
    public Result<List<String>> checkConflicts(@Valid @RequestBody AutoScheduleDto autoScheduleDto) {
        List<String> conflicts = scheduleService.checkScheduleConflicts(autoScheduleDto);
        return Result.success(conflicts);
    }
}